package com.yxs.wechatdemo.service.impl;

import com.yxs.wechatdemo.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/10/10 0010.
 */
@Service
public class RedisServiceImpl implements RedisService {

    /**
     * 过期时间.
     */
    private Integer expire = EXPIRE;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String setRedis(String value) {

        /** 添加数据进redis */
        String uuid = UUID.randomUUID().toString();
        String token = String.format(TOKEN_PREFIX, uuid);

        /**
         * String.format(RedisConstant.TOKEN_PREFIX, token)  token名称以token_%为前缀拼接上token
         * baseInfoBean.getOpenid() 保存的token内容
         * expire 过期时间
         * TimeUnit.SECONDS 设置过期时间的单位为秒
         **/
        redisTemplate.opsForValue().set(token, value, expire, TimeUnit.SECONDS);

        return token;
    }

    @Override
    public void setRedis(String key, String value) {
        redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
    }

    @Override
    public String getRedis(String key) {
        return redisTemplate.opsForValue().get(key);
    }


}
